<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <link rel="stylesheet" href="css/reset.css" />
  <link rel="stylesheet" href="css/main.css" />
  <script type="text/javascript" src="./lib/jquery-1.12.4.min.js"></script>
  <script type="text/javascript" src="./lib/jquery-ui.min.js"></script>
  <script type="text/javascript" src="./lib/jquery.mousewheel.js"></script>
  <script type="text/javascript" src="./lib/scroll.js"></script>
  <title>聊天机器人</title>
</head>

<body>
  <div class="wrap">
    <!-- 头部 Header 区域 -->
    <div class="header">
      <h3>贵儿桑</h3>
      <img src="img/微信图片_20221229164823.jpg" alt="icon" />
    </div>
    <!-- 中间 聊天内容区域 -->
    <div class="main">
      <ul class="talk_list" style="top: 0px;">
        <li class="left_word">
          <img src="img/微信图片_20221229164823.jpg" />
          <span>空你吉瓦</span>
        </li>
        <li class="right_word">
          <img src="img/person02.png" />
          <span>贵桑故乡的樱花开了，你要回去看吗</span>
        </li>
      </ul>
      <!-- 右侧滚动条 -->
      <div class="drag_bar" style="display: none;">
        <div
             class="drager ui-draggable ui-draggable-handle"
             style="display: none; height: 412.628px;"></div>
      </div>
    </div>
    <!-- 底部 消息编辑区域 -->
    <div class="footer">
      <img src="img/person02.png" alt="icon" />
      <!-- 输入框 -->
      <input type="text" placeholder="说的什么吧..." class="input_txt" />
      <!-- 发送按钮 -->
      <input type="button" value="发 送" class="input_sub" />
    </div>
  </div>

  <!-- 导入axios -->
  <script src="./lib/axios.js"></script>
  <script>
    function chat(){
      const value = document.querySelector('.input_txt').value.trim()
      if(value){
        const li = document.createElement('li')
        li.className = 'right_word'
        li.innerHTML = `<img src="img/person02.png" /><span>${value}</span>`
        document.querySelector('.talk_list').appendChild(li)
        document.querySelector('.input_txt').value = ''

        // 第二步：请求接口，创建聊天机器人 的聊天区域（左侧）
        axios
            .get('http://ajax-api.itheima.net/api/robot', {
              params: {
                spoken: value,
              },
            })
            .then((res) => {
              console.log(res.data.data.info.text)
              const myLi = document.createElement('li')
              myLi.className = 'left_word'
              myLi.innerHTML = `<img src="img/微信图片_20221229164823.jpg" />
            <span>${res.data.data.info.text}</span>`

              document.querySelector('.talk_list').appendChild(myLi)
              resetui()
            })
            .catch((error) => {
              console.log(error)
            })
      }
    }
    document.querySelector('.input_sub').addEventListener('click',()=>{
      chat()
    })
    document.querySelector('.input_txt').addEventListener('keyup',(e)=>{
      if(e.keyCode === 13){
        chat()
      }
    })
  </script>
</body>

</html>